import io.lettuce.core.RedisClient;
import io.lettuce.core.RedisURI;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisStringCommands;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

public class Redis {
    private RedisClient redisClient;
    private StatefulRedisConnection<String, String> connect;

    @Before
    public void beforeTest() {
        RedisURI uri = RedisURI.builder().withHost("localhost").withPort(6379).build();
        redisClient = RedisClient.create(uri);
        System.out.println("redis init");
    }

    @Test
    public void string() {
        connect = redisClient.connect();
        RedisStringCommands<String, String> sync = connect.sync();
        sync.set("username", "read");
        //key 值相同则实现修改的功能
        sync.set("username", "write");
        //获取数据get 用 key取value值
        String value = sync.get("username");
        System.out.println(value);
        //删除值
        connect.sync().del("username");
    }

    @Test
    public void hash() {
        connect = redisClient.connect();
        connect.sync().hset("hashset", "set", "test");
        connect.sync().hset("hashset", "user", "test1");
        // hdel删除数据
        connect.sync().hdel("hashset", "user");
        connect.sync().hset("hashset", "set2", "test2");
        System.out.println(connect.sync().hgetall("hashset"));
    }

    @Test
    public void list() {
        connect = redisClient.connect();
        //添加多值 lpush
        connect.sync().lpush("list", "1", "2", "3", "4");
        //输出list里面的数据　逆序输出,先进入的后输出;后进入的先输出
        System.out.println(connect.sync().lrange("list", 0, 3));
        //lpush是链表左边添加
        connect.sync().lpush("list", "5", "6");
        //lpop　从链表的最左边删除一位数据
        connect.sync().lpop("list");
        System.out.println(connect.sync().lrange("list", 0, 5));
        //截取范围内值：ltrim
        System.out.println(connect.sync().ltrim("list", 3, 5));
    }


    @Test
    public void set() {
        connect = redisClient.connect();
        //添加数据
        connect.sync().sadd("set", "1", "2", "3", "4");
        //随机删除里面的数据
        connect.sync().spop("set");
        //查询set数据，输出set里面全部的数据(无序的输入)
        System.out.println(connect.sync().smembers("set"));
        //在set里面查询特定元素,如果set里面有这个元素，则返回true　否则返回false
        System.out.println(connect.sync().sismember("set", "3"));
    }

    @Test
    public void sortedset() {
        connect = redisClient.connect();
        //有序排序　
        connect.sync().zadd("sorted", 1, "test1");
        connect.sync().zadd("sorted", 2, "test2");
        connect.sync().zadd("sorted", 1, "test3");
        connect.sync().zadd("sorted", 3, "test4");
        //获取sorted set 集合数据的长度
        Long sorted = connect.sync().zcard("sorted");
        System.out.println(connect.sync().zrange("sorted", 0, sorted));
        //删除指定元素
        connect.sync().zrem("sorted","test1");
        //输出数据　输出的数据会按照v值的大小排序(由小到大)
        System.out.println(connect.sync().zrange("sorted", 0, sorted));
    }

    @After
    public void After() {
        connect.close();
        redisClient.shutdown();
    }
}